package data;

// 路径规划器（策略模式）
public class PathPlanner {
    // 计算路径
    public Path calculatePath(FieldMap fieldMap, String start, String end) {
        Path path = new Path();
        path.addPoint(start);

        // 简单模拟路径计算
        int startX = start.charAt(0) - 'A';
        int startY = Character.getNumericValue(start.charAt(1)) - 1;

        int endX = end.charAt(0) - 'A';
        int endY = Character.getNumericValue(end.charAt(1)) - 1;

        // 水平移动
        while (startX != endX) {
            if (startX < endX) startX++;
            else startX--;

            String point = "" + (char)('A' + startX) + (startY + 1);
            path.addPoint(point);
        }

        // 垂直移动
        while (startY != endY) {
            if (startY < endY) startY++;
            else startY--;

            String point = "" + (char)('A' + startX) + (startY + 1);
            path.addPoint(point);
        }

        return path;
    }

    // 重新规划路径（避障）
    public Path replanPath(FieldMap fieldMap, Path originalPath) {
        Path newPath = new Path();
        newPath.addPoint(originalPath.points.get(0)); // 起点

        // 简单模拟避障逻辑
        for (int i = 1; i < originalPath.points.size(); i++) {
            String point = originalPath.points.get(i);
            int x = point.charAt(0) - 'A';
            int y = Character.getNumericValue(point.charAt(1)) - 1;

            // 如果遇到障碍物，绕行
            if (!fieldMap.isPassable(x, y)) {
                newPath.addPoint("绕过障碍物");
            } else {
                newPath.addPoint(point);
            }
        }

        return newPath;
    }
}
